package com.momo.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * @author ：朱博
 * @description：TODO
 * @date ：2021/3/21 14:50
 */
public class KafkaTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties props = new Properties();

        //kafka 集 群 ，broker-list
        props.put("bootstrap.servers", "192.168.0.103:9092");

        //ACK应答级别
        props.put("acks", "all");

        //重试次数
        props.put("retries", 1);

        //批次大小 16K
        props.put("batch.size", 16384);

        //等待时间 除了批次大小和时间 都满足我都会发送消息
        props.put("linger.ms", 1);

        //RecordAccumulator 缓冲区大小
        props.put("buffer.memory", 33554432);

        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        /**
         * 生产对象
         */
        Producer<String, String> producer = new KafkaProducer<String, String>(props);

        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<String, String>("mytopic", Integer.toString(i), Integer.toString(i)));
        }

        producer.close();
    }
}
